package com.touchstone.cloudpacs.common.interceptor;

import com.touchstone.cloudpacs.common.constant.UserConstant;
import com.touchstone.cloudpacs.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description : 在线状态拦截器
 * @Author : hunter
 * @Date: 2019-07-24 22:58
 * @Version: 1.0
 **/
@Slf4j
public class OnlineInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("==============================================================");
        log.info("在线状态拦截器开始执行...");
        log.info("来访请求地址【{}】", request.getRequestURL());
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(UserConstant.CURRENT_USER);
        if (user == null) {
            //未在线，跳转登录页面
            response.sendRedirect("/login");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
